package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.TravelGroupDao;
import com.atguigu.pojo.TravelGroup;
import com.atguigu.pojo.TravelItem;
import com.atguigu.service.TravelGroupService;
import com.atguigu.utils.CascadeDownLoadUtils;
import com.atguigu.utils.DownLoadUtils;
import com.atguigu.vo.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author zhengkaiyuan
 * @version 1.0
 * @create 2021-06-21 10:40
 */
@Service(interfaceClass = TravelGroupService.class)
@Transactional
public class TravelGroupServiceImpl implements TravelGroupService {

    @Autowired
    private TravelGroupDao travelGroupDao;

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(currentPage, pageSize);
        //获取所有查询到的数据
        Page<TravelGroup> groups = travelGroupDao.findPage(queryString);
        //返回查询结果
        return new PageResult(groups.getTotal(), groups);

    }

    @Override
    public void add(TravelGroup travelGroup, Integer[] travelItemIds) {
        //将TravelGroup添加到数据库当中
        travelGroupDao.add(travelGroup);
        //注意，需要配置，获取id
        Integer travelGroupId = travelGroup.getId();
        if (travelItemIds != null && travelItemIds.length > 0) {
            //向中间表添加数据
            travelGroupDao.setTravelGroupAndTravelItem(travelGroupId, travelItemIds);
        }

    }

    @Override
    public void delete(Integer id) {

        Integer count = travelGroupDao.findCountByTravelGroupId(id);
        if (count > 0) {
            throw new RuntimeException("删除跟团游失败，原因：存在关联数据" + count + "条");
        } else {
            //需要先删除中间表当中关联的自由行信息
            travelGroupDao.deleteTravelGroupAndTravelItem(id);
            //删除跟团游信息
            travelGroupDao.delete(id);
        }

    }


    @Override
    public TravelGroup findById(Integer id) {
//        1.首先查出当前跟团游信息
        TravelGroup travelGroup = travelGroupDao.findById(id);
        return travelGroup;
    }

    @Override
    public List<Integer> findTravelItemIdByTravelgroupId(Integer id) {
        List<Integer> findTravelItemIdByTravelgroupId = travelGroupDao.findTravelItemIdByTravelgroupId(id);
        return findTravelItemIdByTravelgroupId;
    }

    @Override
    public void edit(TravelGroup travelGroup, Integer[] travelItemId) {
        //1.编辑当前跟团游的一些基本信息
        travelGroupDao.edit(travelGroup);
        //2.处理当前自由行和跟团游的信息
        editTravelItem(travelGroup.getId(), travelItemId);
    }

    @Override
    public void editTravelItem(Integer id, Integer[] travelItemId) {
        //1.首先删除当前id所对应的所有自由行项目
        travelGroupDao.deleteTravelGroupAndTravelItem(id);
        //2.将后台传过来的新的自由行项目添加进去
        travelGroupDao.editGroupItem(id, travelItemId);
    }

    @Override
    public List<TravelGroup> findAll() {
        return travelGroupDao.findAll();
    }

    @Override
    public String downLoadFile(String filePath, String queryString) throws Exception {
        if ("null".equals(queryString)) {
            queryString = "";
        }
        //查询出当前数据
        List<TravelGroup> result = travelGroupDao.findByConditionAndTravelItem(queryString);
        System.out.println("result = " + result);
        //获取类元信息
        String excelName = "travelGroup";
        String excelToName = CascadeDownLoadUtils.createExcelToName(excelName, result, filePath);
        return excelToName;
    }
}
